//
//  JDReleaseRecordViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/5.
//

import UIKit
// 龙商圈->圈内商讯->发布记录
class JDReleaseRecordViewController: JDBaseViewController {

    // 暂时不区分这个吧 确认后再说
    // 0: 圈内商讯  1: 商务快讯
    var type: Int = 0
    
    var mainTableView: UITableView?
    
    var releaseRecordModels: [JDRecruitModel?]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        self.getNetData()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupNavigationTitle("发布记录")
        self.setupLeftBackItem()
    }
    
    func setupUIElements() {
        self.mainTableView = UITableView(frame: .zero, style: .plain)
        self.view.addSubview(self.mainTableView!)
        self.mainTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.top.equalTo(self.view).offset(statusBarHeight+44)
        })
        self.mainTableView?.register(JDCityUnionMessageTableViewCell.self, forCellReuseIdentifier: "myCell")
        self.mainTableView?.backgroundColor = self.commonColor
        self.mainTableView?.delegate = self
        self.mainTableView?.dataSource = self
        self.mainTableView?.separatorStyle = .none
        
        self.mainTableView?.addRefreshHeader { [weak self] in
            self?.currentPage = 1
            self?.getNetData(true)
        }
        
        self.mainTableView?.addRefreshFooter { [weak self] in
            self?.currentPage += 1
            self?.getNetData(false)
        }
        
    }
    
    override func getNetData(_ isRefresh: Bool = true) {
        var params = [String: Any]()
        params["pageNumber"] = self.currentPage
        params["pageSize"] = 10
        JDNetWorkRequest(.getReleaseNewsRecord(params)) { responseStr in
            
            printLog("发布记录:\(responseStr)")
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                if let models = [JDRecruitModel].deserialize(from: responseStr, designatedPath: "data.records") {
                    
                    if isRefresh {
                        // 刷新
                        self.releaseRecordModels = models
                    } else {
                        // 加载
                        self.releaseRecordModels?.append(contentsOf: models)
                    }
                    if models.count < 10 {
         
                        self.mainTableView?.reloadData()
                        self.mainTableView?.noMoreData()
                        return;
                    }
                }
            }
            self.mainTableView?.reloadData()
            self.mainTableView?.endRefresh()
            
        } failure: { error in
            self.mainTableView?.reloadData()
            self.mainTableView?.endRefresh()
        } error: { errorCode in
            self.mainTableView?.reloadData()
            self.mainTableView?.endRefresh()
        }

    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDReleaseRecordViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.releaseRecordModels?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as? JDCityUnionMessageTableViewCell
         
        let currentModel = self.releaseRecordModels?[indexPath.row]
        
        cell?.refreshData(currentModel)
        cell?.messageButton?.isHidden = true
        cell?.callButton?.isHidden = true
        cell?.chatRecordButton?.isHidden = true
        cell?.recordImgV?.isHidden = true
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}
